package com.zhou.uploadServer.controller;

import com.mysql.cj.util.StringUtils;
import com.zhou.exception.UpException;
import com.zhou.exception.UpExceptionEnum;
import com.zhou.uploadServer.resp.FileUploadResp;
import com.zhou.uploadServer.resp.UploadForUUid;
import com.zhou.uploadServer.resp.common.ApiRestResponse;
import com.zhou.uploadServer.model.FileUpload;
import com.zhou.uploadServer.service.FileUploadService;
import com.zhou.utils.CopyUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.text.SimpleDateFormat;

/**
 * @ClassName: UploadController
 * @Description:
 * @Author: admin
 * @Date: 2021/12/6 16:42
 */
@RestController
@RequestMapping("/qys")
public class UploadController {

    @Resource
    private FileUploadService fileUploadService;

    @PostMapping("/upload")
    public ApiRestResponse fileupload(MultipartFile file) throws FileNotFoundException {
        FileUpload fileUpload = fileUploadService.fileUpload(file);

        //进行文件信息入库
        fileUploadService.ins(fileUpload);

        //获取uuid
        if (StringUtils.isNullOrEmpty(fileUpload.getNewName())) {
            throw new UpException(UpExceptionEnum.UPLOAD_FAILID);
        }
        UploadForUUid uploadForUUid = CopyUtil.copy(fileUpload, UploadForUUid.class);
        return ApiRestResponse.success(uploadForUUid);
    }

    @GetMapping("/getfile")
    @ApiOperation("获取文件")
    public void getUrlFile(String fileName, HttpServletRequest request, HttpServletResponse response) throws IOException {
        //创建输出流
        OutputStream os=null;
        //获取文件信息
        FileUploadResp fileData = fileUploadService.getFileData(fileName);

        //拼接文件路径
        String path = fileData.getSaveFolder() + fileName + fileData.getFileType();
        try {
            File file = new File(path);
            String filename = file.getName();// 获取文件名称
            InputStream fis = new BufferedInputStream(new FileInputStream(path));
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();
            response.reset();
            // 先去掉文件名称中的空格,然后转换编码格式为utf-8,保证不出现乱码,这个文件名称用于浏览器的下载框中自动显示的文件名
            response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.replaceAll(" ", "").getBytes("utf-8"), "iso8859-1"));
            response.addHeader("Content-Length", "" + file.length());
            os = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/octet-stream");
            os.write(buffer);// 输出文件

        } catch (Exception e) {
            throw new UpException(UpExceptionEnum.SYSTEM_EXCEPTION);
        }finally {
            os.flush();
            os.close();
        }
    }

    @ApiOperation("获取文件信息")
    @GetMapping("/getdata")
    public ApiRestResponse getData(String fileName) {
        //查询文件信息
        FileUploadResp fileData = fileUploadService.getFileData(fileName);

        return ApiRestResponse.success(fileData);
    }
}
